import java.lang.reflect.Array;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

import org.junit.Test;

import com.situ.study.entity.User;

public class Client {
	// main
	public static void main(String[] args) {
		User user = new User();
		user.setId(1);
		user.setName("张三");
		user.setAge(120);
		user.setGender("男");
		System.out.println(user);
		
		// test1();
		// test2();
	}
	
	@Test
	public void test1() {
		User[] array = new User[10];
		// 解决数据长度固定问题
		ArrayList<User> list = new ArrayList<>();
		Scanner scanner = new Scanner(System.in);
		
		while (true) {
			// Unreachable code
			System.out.println("请输入id：");
			int id = scanner.nextInt();
			System.out.println("请输入用户名：");
			// 读取控制台字符串
			String name = scanner.next();
			System.out.println("请输入年龄：");
			int age = scanner.nextInt();
			System.out.println("请输入性别：");
			String gender = scanner.next();
			// syso
			User user = new User(id, name, age, gender);
			// 默认会调用toString()方法
			// System.out.println(user);
			// 将user对象放到ArrayList
			list.add(user);
			
			System.out.println("输入0退出，-1继续");
			int exit = scanner.nextInt();
			if (exit == 0) {
				break;
			}
		}
		
		// 打印所有用户信息
		for (int i = 0; i < list.size(); i ++) {
			User user = list.get(i);
			System.out.println(user);
		}
		// foreach
		for (User user : list) {
			System.out.println(user);
		}
		
		// 
		while (true) {
			System.out.println("根据id查找：1");
			System.out.println("根据名字查找：2");
			System.out.println("根据性别查找：3");
			System.out.println("输入0退出");
			int type = scanner.nextInt();
			switch (type) {
			case 2:// 根据名字查找
				
				break;

			default:
				break;
			}
		}
		
	}
	
	@Test
	public void test2() {
		// Alt+Shift+A
		// 1、加载驱动Class.forName("");
		try {
			Class.forName("com.mysql.jdbc.Driver");
			// 2、获得连接对象Connection
			Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/taishan?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
			// 3、写sql语句
			String sql = "SELECT id,name,age,gender FROM user";
			// 4、创建Statement
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			// 5、执行sql语句
			//      (1) 更新类（更改了表里面数据）：delete/update/insert     executeUpdate()
			//            返回值：int，表示你影响的行数
			//      (2)查询（没有改变表里面数据）:  select                  executeQuery()
			//            返回值：结果集ResultSet
			ResultSet resultSet = preparedStatement.executeQuery();
			ArrayList<User> list = new ArrayList<>();
			// 判断有没有下一个
			while (resultSet.next()) {
				// 遍历每一行，一个字段一个字段拿出对应的数据，
				// 最终将这些数据封装成一个User对象
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				User user = new User(id, name, age, gender);
				list.add(user);
			}
			for (User user : list) {
				System.out.println(user);
			}
			// 6、关闭连接
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testInsert() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/taishan?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
			String sql = "INSERT INTO user(name,age,gender) VALUES(?,?,?);";
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			// 设置?对应的值
			preparedStatement.setString(1, "王五");
			preparedStatement.setInt(2, 24);
			preparedStatement.setString(3, "男");
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
			// 6、关闭连接
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testDelete() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/taishan?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
			String sql = "DELETE FROM user WHERE id=?";
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			// 设置?对应的值
			preparedStatement.setInt(1, 5);
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
			// 6、关闭连接
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void test3() {
		System.out.println("Client.test3()");
	}
	
	
	

}
